package com.lagou.edu.controller;

import com.lagou.edu.common.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/auth")
public class LoginController {
    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String processLogin(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession(true);
        if (StringUtils.endsWithIgnoreCase(username,"admin") &&
                StringUtils.endsWithIgnoreCase(password,"admin")){
            session.setAttribute(Constant.CURRENT_USER,username);
            return "redirect:/resume/index";
        }else {
            session.removeAttribute(Constant.CURRENT_USER);
            return "redirect:/auth/login?error=login_error";
        }
    }

}
